/*******************************************************************************
 * Copyright (C) 2021 ShangHai Quicktron Intelligent Technology Co.,Ltd
 * All Rights Reserved.
 * 本软件为上海快仓智能科技开发研制。未经本公司正式书面同意，其他任何个人、团体
 * 不得使用、复制、修改或发布本软件.
 * 版权所有 翻版必究
 ******************************************************************************/
package com.vip.seata.at.business.confiigurations;

import feign.Feign;
import feign.RequestInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.SpringProperties;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.Locale;

/**
 * Feign配置类
 *
 * @author kim.cheng
 * @history 2019-3-18
 */
@Slf4j
@Configuration
@ConditionalOnClass({Feign.class})
public class FeignConfiguration {

    String FEIGN_Source_Application = "Feign-Source-Application";
    String FEIGN_Caller_Application = "Feign-Caller-Application";

    @Bean
    public RequestInterceptor headerInterceptor() {
        return template -> {
            String application = SpringProperties.getProperty("spring.application.name");
            template.header(FEIGN_Source_Application,application);
            template.header(FEIGN_Caller_Application,application);
            String url = template.url();
            log.info("Feign URL:{}",url);
        };
    }

    private static class FeignRequestMappingHandlerMapping extends RequestMappingHandlerMapping {
        @Override
        protected boolean isHandler(Class<?> beanType) {
            return super.isHandler(beanType) && !AnnotatedElementUtils.hasAnnotation(beanType, FeignClient.class);
        }
    }
}